package dev.polls.skycube;

import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ListActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class GroupList extends ListActivity
{
	GetJSon gj = new GetJSon();
	String[] groups = new String[10];
	int groupcount = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  
	
	  
	  try {
		setListAdapter(new ArrayAdapter<String>(this, R.layout.list_view, grouplist()));
	} catch (ClientProtocolException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (URISyntaxException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (JSONException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	  ListView lv = getListView();
	  lv.setTextFilterEnabled(true);

	  lv.setOnItemClickListener(new OnItemClickListener() {
	    public void onItemClick(AdapterView<?> parent, View view,
	        int position, long id) {
	      // When clicked, show a toast with the TextView text
	      Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
	          Toast.LENGTH_SHORT).show();
	    }
	  });
	}
	 public String[] grouplist() throws ClientProtocolException, URISyntaxException, IOException, JSONException
	 {
		 SharedPreferences settings = getSharedPreferences("account_info",0);
		 String username = settings.getString("username", "");
		 String password = settings.getString("password", "");
		 
		 
		 
		 JSONArray ja = gj.getGroupList(username, password);
		 for (int i = 0; i < 4; ++i) 
		 {
			 JSONObject rec = ja.getJSONObject(i);
		     groups[i] = rec.getString("Name");
		 }
		 return groups;
	 }
	 public void setGroups(int i)
	 {
		 groupcount = i;
	 }
	 public int getGroups()
	 {
		 return groupcount;
	 }
}